package com.mogujie.tt.customview.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.mogujie.tt.R;

/**
 * Created by user on 2015/9/11.
 */
public class CustomDot extends View {
    Paint mPaint;//画笔,包含了画几何图形、文本等的样式和颜色信息
    Rect mBound;


    public CustomDot(Context context){
        super(context);

    }

    public CustomDot(Context context,AttributeSet attrs){
        super(context,attrs);
        mPaint=new Paint();
        //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
        //在使用完成后，一定要调用recycle方法
        //属性的名称是styleable中的名称+“_”+属性名称
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.CustomDot);
        int textColor=array.getColor(R.styleable.CustomDot_textColor, 0XFF00FF00);//提供默认值，放置未指定
        mPaint.setColor(textColor);
        array.recycle();//一定要调用，否则这次的设定会对下次的使用造成影响
        mBound =new Rect(0, 0, 60,60);//先设置一个区域

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 0;
        int height = 0;

        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (specMode)
        {
            case MeasureSpec.EXACTLY:// 明确指定了
                width = specSize;//比设置的宽度大一些 设置的值会认为是内容
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                width =  mBound.width();
                break;
        }

        /**
         * 设置高度
         */
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (specMode)
        {
            case MeasureSpec.EXACTLY:// 明确指定了
                height =  specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                height =  mBound.height();
                break;
        }

        setMeasuredDimension(width, height);

    }

    @Override
    public void onDraw(Canvas canvas){
        //  super.onDraw(canvas);

        int radius =getMeasuredWidth() / 2;

        mPaint.setStyle(Paint.Style.FILL); //设置填充

        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, radius, mPaint);
    }

    public void setColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    public int getColor(){
        return mPaint.getColor();
    }
}
